<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use DB;
class LinkController extends Controller
{
     public function index(Request $request)
    {
    	$where = [];
        //实例化要操作的表
        $db = DB::table("links");
        //判断有没有搜索name字段的值
        if($request->has('name')){
            // 获取要搜索的name字段的值
            $name = $request->input('name');
            //添加where模糊匹配条件
            $db->where('name','like',"%{$name}%");
            $where['name'] = $name;
        }
        // 分页
        $list = $db->paginate(6);
        //加载模板
        return view('admin.links.link',['list'=>$list,'where'=>$where]);
    }

    public function create()
    {
    	return view('admin.links.add');
    }

    public function store(Request $request)
    {
    	$data = $request->except('_token');
    	$id = DB::table('links')->insertGetId($data);
    	if($id>0){
    		return redirect('admin/link');
    	}
    }

    public function destroy($id)
    {
    	$row = DB::table('links')->where('id',$id)->delete();
    	if($row>0){
    		return redirect('admin/link');
    	}
    }
}
